package com.moana.buyTicket.servlet;

import java.io.IOException;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.Logger;
import org.datanucleus.store.rdbms.mapping.VarBinaryRDBMSMapping;
import org.springframework.http.HttpRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.google.gson.JsonObject;
import com.moana.buyTicket.entity.Person;

@Controller
public class LoginAction {
	
	private static String URL = "https://graph.qq.com/user/get_user_info?accessToken=%s&oauth_consumer_key=%s&openid=%s";
	private static Logger logger = Logger.getLogger(LoginAction.class); 
	
	@RequestMapping(value="/login.htm")
	public static String login(HttpServletRequest request,HttpServletResponse response){
		String access_token = request.getParameter("access_token");
		String openId = request.getParameter("openId");
		String appID = request.getParameter("appID");
		HttpClient client = new HttpClient();
		GetMethod method = new GetMethod();
		String urlStr = String.format(URL, access_token,openId,appID);
		method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
				new DefaultHttpMethodRetryHandler());
		try {
			// 执行getMethod
			int statusCode = client.executeMethod(method);
			if (statusCode != 200) {
				System.err.println("Method failed: "
						+ method.getStatusLine());
			}
			// 读取内容
			String responseBody = method.getResponseBodyAsString();
			logger.debug("获取response："+response);
			
			ObjectMapper objectMapper = new ObjectMapper();
			JsonNode tree = objectMapper.readTree(responseBody); 
			Person user = new Person();
			String nickname = tree.get("nickname").textValue();
			user.setId(Integer.valueOf(openId));
			user.setName(nickname);
			HttpSession session = request.getSession();
			session.setAttribute("user", user);
		} catch (HttpException e) {
			// 发生致命的异常，可能是协议不对或者返回的内容有问题
			System.out.println();
			e.printStackTrace();
			return "error.jsp";
		} catch (IOException e) {
			// 发生网络异常
			e.printStackTrace();
			return "error.jsp";
		} finally {
			// 释放连接
			method.releaseConnection();
		}
		
		return "welcome.jsp";
	}
	
	//测试登陆入口
	@RequestMapping(value="/loginTest.htm")
	public static String loginTest(HttpServletRequest request,HttpServletResponse response){
		
		Person user = new Person();
		user.setId(111);
		user.setName("测试1");
		HttpSession session = request.getSession();
		session.setAttribute("user", user);
		return "welcome.jsp";
	}	
}
